// 新增
$(function () {
    $("#submit").click(function (event) {
        event.preventDefault();
        var case_nameE = $("input[name=case_name]");
        var case_urlE = $("textarea[name=case_url]");
        var case_paramE = $("textarea[name=case_param]");
        var expect_resE = $("input[name=expect_res]");
        var headersE = $("textarea[name=header]");
        var remarkE = $("textarea[name=remark]");
        var request_methodE = $("#request_method option:checked");
        var groupE = $("#group option:checked");

        var case_name = case_nameE.val();
        var case_url = case_urlE.val();
        var case_param = case_paramE.val();
        var expect_res = expect_resE.val();
        var header = headersE.val();
        var remark = remarkE.val();
        var request_method = request_methodE.val();
        var group = groupE.val();
        var status = $('input[name="status"]:checked').val();

        if(!case_name || !case_url || !request_method || !group || !expect_res){
            amanalert.alertInfo("有必填项为空，请检查！");
            return;
        }
        var expect_res_list = expect_res.split("=");
        var tem = ["and", "or", "not"];
        var tem1 = expect_res_list[0].toLowerCase();
        if(expect_res_list.length == 2 && tem.indexOf(tem1) > -1 && expect_res_list[1] == ''){
            amanalert.alertInfo("预期结果设置逻辑匹配（or、and、not），等号后面不能为空！");
            return;
        }

        amanajax.post({
            'url':'/addcase/',
            'data':{
                'case_name':case_name,
                'case_url':case_url,
                'case_param':case_param,
                'expect_res':expect_res,
                'headers':header,
                'remark':remark,
                'request_method':request_method,
                'group':group,
                'status':status
            },
            'success':function (data) {
                // console.log(data);
                if(data['code'] == 1){
                    amanalert.alertSuccessToast("添加成功，可以继续添加！");
                    case_nameE.val("");
                    case_urlE.val("");
                    case_paramE.val("");
                    expect_resE.val("");
                    headersE.val("");
                    remarkE.val("");
                }else {
                    var message = data['message'];
                    amanalert.alertInfo(message);
                }
            },
            'fail':function (error) {
                amanalert.alertNetworkError()
            }
        });

    });
});

//调试
$(function () {
    $("#debug-case-btn").click(function (event) {
        event.preventDefault();
        var self = $(this);
        var dialog = $("#editcase-dialog");
        //获取元素
        var case_nameE = $("input[name=case_name]");
        var case_urlE = $("textarea[name=case_url]");
        var case_paramE = $("textarea[name=case_param]");
        var expect_resE = $("input[name=expect_res]");
        var headerE = $("textarea[name=header]");
        var remarkE = $("textarea[name=remark]");

        var case_name = case_nameE.val();
        var case_url = case_urlE.val();
        var case_param = case_paramE.val();
        var expect_res = expect_resE.val();
        var header = headerE.val();
        var remark = remarkE.val();
        var request_methodE = $("#request_method option:checked");
        var request_method = request_methodE.val();
        var groupE = $("#group option:checked");
        var group = groupE.val();

        var case_id = self.attr("data-id"); //编辑的时候动态绑定该属性

        if(!case_url || !request_method || !expect_res){
            amanalert.alertInfo("调试必须填写url、请求方法以及预期结果！");
            return;
        }

        var expect_res_list = expect_res.split("=");
        var tem = ["and", "or", "not"];
        var tem1 = expect_res_list[0].toLowerCase();
        if(expect_res_list.length == 2 && tem.indexOf(tem1) > -1 && expect_res_list[1] == ''){
            amanalert.alertInfo("预期结果设置逻辑匹配（or、and、not），等号后面不能为空！");
            return;
        }

        amanajax.post({
            'url':'/debugcase/',
            'data':{
                'case_name':case_name,
                'case_url':case_url,
                'case_param':case_param,
                'expect_res':expect_res,
                'headers':header,
                'remark':remark,
                'request_method':request_method,
                'group':group,
                'case_id':case_id
            },
            'success':function (data) {
                console.log(data);
                if(data['code'] == 1){
                    amanalert.alertInfo(data['message']);
                }else {
                    var message = data['message'];
                    amanalert.alertInfo(message);
                }
            },
            'fail':function (error) {
                amanalert.alertNetworkError()
            }
        });
    });
});

// 编辑
$(function () {
    $("#save-case-btn").click(function (event) {
        event.preventDefault();
        var self = $(this);
        var dialog = $("#editcase-dialog");

        //获取元素
        var case_nameE = $("input[name=case_name]");
        var case_urlE = $("textarea[name=case_url]");
        var case_paramE = $("textarea[name=case_param]");
        var expect_resE = $("input[name=expect_res]");
        var headerE = $("textarea[name=header]");
        var remarkE = $("textarea[name=remark]");

        var status = $('input[name="status"]:checked').val();
        var case_name = case_nameE.val();
        var case_url = case_urlE.val();
        var case_param = case_paramE.val();
        var expect_res = expect_resE.val();
        var header = headerE.val();
        var remark = remarkE.val();

        // 获取下拉选项的内容
        var request_methodE = $("#request_method option:checked");
        var groupE = $("#group option:checked");
        var request_method = request_methodE.val();
        var group = groupE.val();

        var case_id = self.attr("data-id"); //编辑的时候动态绑定该属性

        if(!case_name || !case_url || !request_method || !group || !expect_res){
            amanalert.alertInfo("有必填项为空，请检查！");
            return;
        }
        var expect_res_list = expect_res.split("=");
        var tem = ["and", "or", "not"];
        var tem1 = expect_res_list[0].toLowerCase();
        if(expect_res_list.length == 2 && tem.indexOf(tem1) > -1 && expect_res_list[1] == ''){
            amanalert.alertInfo("预期结果设置逻辑匹配（or、and、not），等号后面不能为空！");
            return;
        }

        amanajax.post({
            'url':'/updatecase/',
            'data':{
                'case_name':case_name,
                'case_url':case_url,
                'case_param':case_param,
                'expect_res':expect_res,
                'headers':header,
                'remark':remark,
                'status':status,
                'request_method':request_method,
                'group':group,
                'case_id':case_id
            },
            'success':function (data) {
                console.log(data);
                if(data['code'] == 1){
                    dialog.modal('hide');
                    amanalert.alertSuccessToast("修改成功！");
                    window.location.reload();
                }else {
                    var message = data['message'];
                    amanalert.alertInfo(message);
                }
            },
            'fail':function (error) {
                amanalert.alertNetworkError()
            }
        });
    });
});

// 编辑页会显原数据
$(function () {
    $(".edit-case-btn").click(function (event) {
        event.preventDefault();
        var self = $(this);
        var dialog = $("#editcase-dialog");

        dialog.modal("show");
        var tr = self.parent().parent();

        var case_name = tr.attr("data-case_name");
        var case_sign = tr.attr("data-case_sign");
        var case_url = tr.attr("data-case_url");
        var case_param = tr.attr("data-case_param");
        var expect_res = tr.attr("data-expect_res");
        var header = tr.attr("data-header");
        var remark = tr.attr("data-remark");
        var status = tr.attr("data-status");
         // 回显下拉菜单选中的选项
        var group = tr.attr("data-group");
        var group_selector = document.getElementById("group");
        for(var i=0;i<group_selector.options.length;i++){
            if(group_selector.options[i].text == group){
                group_selector.options[i].selected = true;
                break;
            }
        }

        var request_method = tr.attr("data-request_method");
        var method_selector = document.getElementById("request_method");
        if(request_method == 'get'){
            method_selector.options[1].selected = true;
        }else {
            method_selector.options[0].selected = true;
        }

        var case_nameE = dialog.find("input[name='case_name']");
        var case_signE = dialog.find("input[name='case_sign']");
        var case_urlE = dialog.find("textarea[name='case_url']");
        var case_paramE = dialog.find("textarea[name='case_param']");
        var expect_resE = dialog.find("input[name='expect_res']");
        var headerE = dialog.find("textarea[name='header']");
        var remarkE = dialog.find("textarea[name='remark']");
        var saveBtn = dialog.find("#save-case-btn");
        var debugBtn = dialog.find("#debug-case-btn");

        case_nameE.val(case_name);
        case_signE.val(case_sign);
        case_urlE.val(case_url);
        case_paramE.val(case_param);
        expect_resE.val(expect_res);
        headerE.val(header);
        remarkE.val(remark);

        if(status == '0'){
            $("#enable").attr("checked", false);
            $("#disable").attr("checked", true);
        }

        saveBtn.attr("data-id", tr.attr("data-id"));
        debugBtn.attr("data-id", tr.attr("data-id"));

    });
});

// 删除
$(function () {
    $(".delete-case-btn").click(function (event) {
        var self = $(this);
        var tr = self.parent().parent();
        var case_id = tr.attr('data-id');
        amanalert.alertConfirm({
            "msg":"确定删除这条用例吗？",
            "confirmCallback":function () {
                amanajax.post({
                    'url':'/delcase/',
                    'data':{
                        'case_id':case_id
                    },
                    'success':function (data) {
                        if(data['code'] == 1){
                            amanalert.alertSuccessToast('删除成功！');
                            window.location.reload();
                        }else {
                            amanalert.alertToast(data['message']);
                        }
                    },
                    'fail':function (error) {
                        amanalert.alertNetworkError();
                    }
                })
            }
        });
    });
});

// 查看
$(function () {
   $(".preview-case-btn").click(function (event) {
        var self = $(this);
        event.preventDefault();
        var dialog = $("#preview-case-dialog");
        dialog.modal("show");

        var tr = self.parent().parent();

        var case_name = tr.attr("data-case_name");
        var case_sign = tr.attr("data-case_sign");
        var case_url = tr.attr("data-case_url");
        var case_param = tr.attr("data-case_param");
        var expect_res = tr.attr("data-expect_res");
        var header = tr.attr("data-header");
        var remark = tr.attr("data-remark");
        var status = tr.attr("data-status");

         // 回显下拉菜单选中的选项
        var group = tr.attr("data-group");
        var group_selector = document.getElementById("preview-group");
        for(var i=0;i<group_selector.options.length;i++){
            if(group_selector.options[i].text == group){
                group_selector.options[i].selected = true;
                break;
            }
        }

        var request_method = tr.attr("data-request_method");
        var method_selector = document.getElementById("preview-method");
        if(request_method == 'get'){
            method_selector.options[1].selected = true;
        }else {
            method_selector.options[0].selected = true;
        }

        if(status == '0'){
            $("#enabled").attr("checked", false);
            $("#disabled").attr("checked", true);
        }

        var case_nameE = dialog.find("input[name='preview-case_name']");
        var case_signE = dialog.find("input[name='preview-case_sign']");
        var case_urlE = dialog.find("textarea[name='preview-case_url']");
        var case_paramE = dialog.find("textarea[name='preview-case_param']");
        var expect_resE = dialog.find("input[name='preview-expect_res']");
        var headerE = dialog.find("textarea[name='preview-header']");
        var remarkE = dialog.find("textarea[name='preview-remark']");
        case_nameE.val(case_name);
        case_signE.val(case_sign);
        case_urlE.val(case_url);
        case_paramE.val(case_param);
        expect_resE.val(expect_res);
        headerE.val(header);
        remarkE.val(remark);

   });
});


// 字符较多时以tip或弹窗展示
$(function () {
  $('[data-toggle="popover"]').popover()
});